/********************************************************************
 * PROJECT: FOLIAGE PROJECT.
 * SUPPLIER: FOLIAGE TEAM.
 ********************************************************************
 * FILE: AbstractAdvancedTask.java
 * (C) Copyright Foliage Team 2013, All Rights Reserved.
 ********************************************************************/
package org.foliage.common.structure.impl;

import org.foliage.common.structure.AdvancedTask;
import org.foliage.common.structure.Task;

/**
 * 具有树形结构严谨的任务对象抽象类，定义任务时间检索等实现方法。
 * 
 * @author deathknight0718@gmail.com
 * @version 1.0.0
 */
public abstract class AbstractAdvancedTask extends AbstractMutableTreeNode implements AdvancedTask {

	// ----------------------------------------------- ATTRIBUTE

	/** 所属任务链实例。 */
	AbstractAdvancedTask parent;
	/** 下属任务或任务链实例数组。 */
	AbstractAdvancedTask[] children;

	// ----------------------------------------------- CONSTRUCTOR

	public AbstractAdvancedTask() {}

	// ----------------------------------------------- METHOD
	
	

	@Override
	public int compareTo(Task task) {
		if (task == null) {
			throw new NullPointerException();
		}
		long thisBeginTime = getBeginCalendar().getTimeInMillis();
		long thisEndTime = getEndCalendar().getTimeInMillis();
		long taskBeginTime = task.getBeginCalendar().getTimeInMillis();
		long taskEndTime = task.getEndCalendar().getTimeInMillis();
		if (taskEndTime < thisBeginTime) {
			return Task.FOLLOWING;
		} else if (taskBeginTime > thisEndTime) {
			return Task.FRONT;
		} else {
			return Task.OVERLAPPING;
		}
	}

	public AbstractAdvancedTask getFrontTask() {
		AbstractAdvancedTask task = null;
		if (getParent() != null && getParent().hasChild()) {
			AbstractAdvancedTask[] brothers = getParent().getChildren();
			for (AbstractAdvancedTask brother : brothers) {
				if (compareTo(brother) == Task.FOLLOWING) {
					if (task == null) {
						task = brother;
					}
					long brotherEndTime = brother.getEndCalendar().getTimeInMillis();
					long taskEndTime = task.getEndCalendar().getTimeInMillis();
					if (brotherEndTime > taskEndTime) {
						task = brother;
					}
				}
			}
		}
		return task;
	}

	public AbstractAdvancedTask getFollowingTask() {
		AbstractAdvancedTask task = null;
		if (getParent() != null && getParent().hasChild()) {
			AbstractAdvancedTask[] brothers = getParent().getChildren();
			for (AbstractAdvancedTask brother : brothers) {
				if (compareTo(brother) == Task.FRONT) {
					if (task == null) {
						task = brother;
					}
					long brotherBeginTime = brother.getBeginCalendar().getTimeInMillis();
					long taskBeginTime = task.getBeginCalendar().getTimeInMillis();
					if (brotherBeginTime < taskBeginTime) {
						task = brother;
					}
				}
			}
		}
		return task;
	}

	// ----------------------------------------------- GETTER&&SETTER

	public AbstractAdvancedTask getParent() {
		return parent;
	}

	public void setParent(AbstractAdvancedTask parent) {
		super.setParent(parent);
	}

	public AbstractAdvancedTask[] getChildren() {
		return children;
	}

	public void setChildren(AbstractAdvancedTask[] children) {
		super.setChildren(children);
	}

}
